const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 时间处理器
const timestampToTime = function (time) {
  // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  if (time) {
    var date = new Date(time)
    var Y = date.getFullYear() + '-'
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
    return Y + M + D
  }
}

/*函数防抖*/
const debounce = function (fn, interval) {
  var timer
  var gapTime = interval || 2000
  return function() {
    clearTimeout(timer)
    var context = this
    var args = arguments
    timer = setTimeout(function() {
      fn.call(context,args)
    }, gapTime)
  }
}
/*函数节流*/
const throttle = function (fn, interval) {
  var enterTime = 0;//触发的时间
  var gapTime = interval || 1000; // 间隔时间，如果interval不传，则默认1s
  return function () {
    var context = this
    var backTime = new Date(); // 第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context, arguments);
      enterTime = backTime // 赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/* 加密、解密算法封装 */
const encode = (input) => {
  // private property
  var _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  // public method for encoding
  var output = ''
  var chr1, chr2, chr3, enc1, enc2, enc3, enc4
  var i = 0
  let string = input
  string = string.replace(/\r\n/g, '\n')
  var utftext = ''
  for (var n = 0; n < string.length; n++) {
    var c = string.charCodeAt(n)
    if (c < 128) {
      utftext += String.fromCharCode(c)
    } else if ((c > 127) && (c < 2048)) {
      utftext += String.fromCharCode((c >> 6) | 192)
      utftext += String.fromCharCode((c & 63) | 128)
    } else {
      utftext += String.fromCharCode((c >> 12) | 224)
      utftext += String.fromCharCode(((c >> 6) & 63) | 128)
      utftext += String.fromCharCode((c & 63) | 128)
    }
  }
  input = utftext
  while (i < input.length) {
    chr1 = input.charCodeAt(i++)
    chr2 = input.charCodeAt(i++)
    chr3 = input.charCodeAt(i++)
    enc1 = chr1 >> 2
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
    enc4 = chr3 & 63
    if (isNaN(chr2)) {
      enc3 = enc4 = 64
    } else if (isNaN(chr3)) {
      enc4 = 64
    }
    output = output +
      _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
      _keyStr.charAt(enc3) + _keyStr.charAt(enc4)
  }
  return output
}

/* 加密、解密算法封装 */
const decode = (input) => {
  // private property
  var _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  var output = ''
  var chr1, chr2, chr3
  var enc1, enc2, enc3, enc4
  var i = 0
  // eslint-disable-next-line no-useless-escape
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
  while (i < input.length) {
    enc1 = _keyStr.indexOf(input.charAt(i++))
    enc2 = _keyStr.indexOf(input.charAt(i++))
    enc3 = _keyStr.indexOf(input.charAt(i++))
    enc4 = _keyStr.indexOf(input.charAt(i++))
    chr1 = (enc1 << 2) | (enc2 >> 4)
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
    chr3 = ((enc3 & 3) << 6) | enc4
    output = output + String.fromCharCode(chr1)
    // eslint-disable-next-line eqeqeq
    if (enc3 != 64) {
      output = output + String.fromCharCode(chr2)
    }
    // eslint-disable-next-line eqeqeq
    if (enc4 != 64) {
      output = output + String.fromCharCode(chr3)
    }
  }
  let utftext = output
  var string = ''
  var j = 0
  let c = 0
  let c2 = 0
  let c3 = 0
  while (j < utftext.length) {
    c = utftext.charCodeAt(j)
    if (c < 128) {
      string += String.fromCharCode(c)
      j++
    } else if ((c > 191) && (c < 224)) {
      // eslint-disable-next-line no-undef
      c2 = utftext.charCodeAt(j + 1)
      string += String.fromCharCode(((c & 31) << 6) | (c2 & 63))
      j += 2
    } else {
      // eslint-disable-next-line no-undef
      c2 = utftext.charCodeAt(j + 1)
      // eslint-disable-next-line no-undef
      c3 = utftext.charCodeAt(j + 2)
      // eslint-disable-next-line no-undef
      string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
      j += 3
    }
  }
  output = string
  return output
}

/**
 * 时间戳转化格式
 */
var dateFormat = function(timestamp, format) {
  var date = getDate(timestamp * 1000)
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  if (month < 10) month = '0' + month
  if (day < 10) day = '0' + day
  var hour = function() {
    if(date.getHours() < 10) {
      return '0' + date.getHours()
    }
    return date.getHours()
  }

  var minute = function() {
    if(date.getMinutes() < 10) {
      return '0' + date.getMinutes()
    }
    return date.getMinutes()
  }

  var second = function() {
    if(date.getSeconds() < 10) {
      return '0' + date.getSeconds()
    }
    return date.getSeconds()
  }
  switch(format) {
    case 'HH:mm':
    return hour() + ':' + minute()
    case 'HH:mm:ss':
    return hour() + ':' + minute() + ':' + second()
    case 'MM-DD HH:mm':
    return month + '-' + day + ' ' + hour() + ':' + minute()
    case 'YY-MM-DD':
    return year + '-' + month + '-' + day
    case 'YY-MM-DD HH:mm':
    return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute()
    case 'MM-DD':
    return month + '-' + day
    case 'YY.MM':
    return year + '.' + month
    case 'YM':
    return year+'年'+month+'月'
  }
}

/**
 * 时间戳转化活跃时间
 */
var activeTime = function(timestamp) {
  var releaseTime = timestamp
  var releaseDate = getDate(releaseTime)
  var now = getDate()
  var timeStr = ''
  if (releaseTime) {
    var differ = parseInt((now.getTime() - releaseTime) / 1000)
    var timeCompany = {
      m: 60,
      h: 60 * 60,
      d: 60 * 60 * 24
    }
    // 是否跨年 and 超过两天
    var nowDate = {
      y: now.getFullYear(),
      m: now.getMonth(),
      d: now.getDate()
    }
    var releaseTimeDate = {
      y: releaseDate.getFullYear(),
      m: releaseDate.getMonth(),
      d: releaseDate.getDate()
    }
    // 是否当天
    if (nowDate.y === releaseTimeDate.y && nowDate.m === releaseTimeDate.m && nowDate.d === releaseTimeDate.d) {
      timeStr = dateFormat(releaseTime / 1000, 'HH:mm')
    } else {
      // 跨年
      if (nowDate.y > releaseTimeDate.y) {
        timeStr = dateFormat(releaseTime / 1000, 'YY-MM-DD  HH:mm')
      } else {
        // 超过两天
        if (nowDate.d - releaseTimeDate.d > 2) {
          timeStr = dateFormat(releaseTime / 1000, 'MM-DD HH:mm')
        } else if (nowDate.d - releaseTimeDate.d === 2) { // 两天
          timeStr = '前天 ' + dateFormat(releaseTime / 1000, 'HH:mm')
        } else if (nowDate.d - releaseTimeDate.d === 1) { // 一天
          timeStr = '昨天 ' + dateFormat(releaseTime / 1000, 'HH:mm')
        }
      }
    }
  }
  return timeStr
}
// 取数组相同部分
const getArrEqual = function (arr1, arr2) {
  let newArr = []
  for (let i = 0; i < arr2.length; i++) {
    for (let j = 0; j < arr1.length; j++) {
      if(arr1[j] === arr2[i]){
        newArr.push(arr1[j])
      }
    }
   }
 return newArr
}
module.exports = {
  formatTime: formatTime,
  timestampToTime: timestampToTime,
  debounce: debounce,
  throttle: throttle,
  encode: encode,
  decode: decode,
  dateFormat: dateFormat,
  activeTime: activeTime,
  getArrEqual: getArrEqual
}
